package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.JoinActivity;
import com.ly.pojo.dto.JoinActivityDTO;
import com.ly.pojo.vo.JoinActivityVO;
import com.ly.service.JoinActivityService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 黑渊白花
 * @ClassName JoinActivityController
 * @date : 2025/2/10 21:47
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/joinActivity")
@Slf4j
public class JoinActivityController {

    @Autowired
    private JoinActivityService joinActivityService;

    //管理员
    @GetMapping("/Allpage")
    public Object Allpage( JoinActivityDTO joinActivityDTO){
        IPage<JoinActivityVO> allJoinActPage = joinActivityService.getAllJoinActPage(joinActivityDTO);
        return Result.success(allJoinActPage);
    }
    //社长
    @GetMapping("/page")
    public Object page( JoinActivityDTO joinActivityDTO){
        IPage<JoinActivityVO> stJoinActPage = joinActivityService.getJoinActPage(joinActivityDTO);
        return Result.success(stJoinActPage);
    }

    //通过joinActI获得该活动的报名人员
    @GetMapping("/pageById")
    public Object pageById( JoinActivityDTO joinActivityDTO){
        log.info("{}",joinActivityDTO);
        IPage<JoinActivityVO> JoinActPageById = joinActivityService.getJoinActPageById(joinActivityDTO);
        return Result.success(JoinActPageById);
    }

    //添加：学生必须存在主要用于社长管理员添加
    @PostMapping("/save")
    public Result save(@RequestBody JoinActivityVO joinActivityVO){
        Boolean ja = joinActivityService.saveJoinAct(joinActivityVO);
        if(ja==false){
            return Result.warn("添加失败，学生已存在或请输入正常的学生名");
        }
        return Result.success("添加成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        Boolean b = joinActivityService.delByid(id);
        if(b){
            return Result.success("删除成功");
        }
        return Result.warn("删除失败！");
    }

    //退出活动
    @PostMapping ("/deleteJoinAct")
    public Result deleteJoinAct(@RequestBody JoinActivity joinActivity){
        Boolean b = joinActivityService.deleteJoinAct(joinActivity);
        if(b){
            return Result.success("删除成功！");
        }
        return Result.warn("删除失败！");
    }

    //该学生是否已经报名该活动
    @GetMapping("/isJoinThisAct")
    @ExcludeToken
    public Result isJoinThisAct( JoinActivityDTO joinActivityDTO){
        Boolean joinThisAct = joinActivityService.isJoinThisAct(joinActivityDTO);
        //1是已经报名该活动
        if(joinThisAct){
            return Result.success(1);
        }
        return Result.success(0);
    }

    //报名活动
    @PutMapping("/signAct")
    public Result signAct(@RequestBody JoinActivity joinActivity){
        String message = joinActivityService.signAct(joinActivity);
        return Result.success(message);
    }

}
